package com.ruoyi.liborder.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.liborder.domain.LibOrderStu;
import com.ruoyi.liborder.service.ILibOrderStuService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/lib/stu")
public class StuController extends BaseController {
    @Autowired
    private ILibOrderStuService libOrderStuService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('lib:stu:list')")
    @GetMapping("/list")
    public TableDataInfo list(LibOrderStu libOrderStu) {
        startPage();
        List<LibOrderStu> list = libOrderStuService.selectLibOrderStuList(libOrderStu);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('lib:stu:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LibOrderStu libOrderStu) {
        List<LibOrderStu> list = libOrderStuService.selectLibOrderStuList(libOrderStu);
        ExcelUtil<LibOrderStu> util = new ExcelUtil<>(LibOrderStu.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('lib:stu:query')")
    @GetMapping(value = "/{stuId}")
    public AjaxResult getInfo(@PathVariable("stuId") String stuId) {
        return AjaxResult.success(libOrderStuService.selectLibOrderStuByStuId(stuId));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('lib:stu:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LibOrderStu libOrderStu) {
        return toAjax(libOrderStuService.insertLibOrderStu(libOrderStu));
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('lib:stu:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LibOrderStu libOrderStu) {
        return toAjax(libOrderStuService.updateLibOrderStu(libOrderStu));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('lib:stu:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stuIds}")
    public AjaxResult remove(@PathVariable String[] stuIds) {
        return toAjax(libOrderStuService.deleteLibOrderStuByStuIds(stuIds));
    }


    /**
     * 绑定学生信息
     */
    @PreAuthorize("@ss.hasPermi('lib:stu:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping("/bind")
    public AjaxResult bind(@RequestBody LibOrderStu libOrderStu) {
        LibOrderStu stu = libOrderStuService.selectLibOrderStuByStuId(libOrderStu.getStuId());
        if (stu.getStuBind() != 0) {
            return AjaxResult.error("该学生信息已被绑定");
        } else if (libOrderStu.getStuRealName().equals(stu.getStuRealName())) {
            SysUser user = userService.selectUserById(getUserId());
            if (!user.getStuId().isEmpty()) {
                return AjaxResult.error("该用户已经绑定过学生信息");
            }
            user.setStuId(libOrderStu.getStuId());
            user.setRoleIds(new Long[]{104L});
            stu.setStuBind(1);
            return toAjax(userService.updateUser(user));
        } else {
            return AjaxResult.error("学号与姓名不一致");
        }
    }


}
